﻿using System;
using BigBank.Contracts.Authorization.V1.Clients;
using BigBank.Contracts.Authorization.V1.Interfaces;
using BigBank.Contracts.Authorization.V1.Services;
using BigBank.Contracts.Integration.V1.Messages;
using BigBank.Contracts.Logging.V1.Clients;
using BigBank.Contracts.Logging.V1.Interfaces;
using BigBank.Contracts.Logging.V1.Services;

namespace BigBank.Contracts.Integration.V1
{
    namespace Services
    {
        partial class IntegrationServiceBase
        {
            public IAuthorizationServiceClient AuthorizationServiceClient { get; set; }

            protected IntegrationServiceBase() : this(
                new AuthorizationServiceClient(new AuthorizationServiceProxy()),
                new LoggingServiceClient(new LoggingServiceProxy()))
            {
            }

            protected IntegrationServiceBase(IAuthorizationServiceClient authorizationServiceClient,
                                             ILoggingServiceClient loggingServiceClient) : base(loggingServiceClient)
            {
                AuthorizationServiceClient = authorizationServiceClient;

                PreIntegrationServiceOperation += OnPreIntegrationServiceOperation;
                
            }

            private void OnPreIntegrationServiceOperation(ref IntegrationServiceRequest request)
            {
                if (!AuthorizationServiceClient.IsAuthorized(request))
                    throw new Exception("Request is not authorized.");
            }
        }
    }
}